package me.azthink.myblog.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.azthink.myblog.dao.ArticleDAO;
import me.azthink.myblog.dao.NavDAO;
import me.azthink.myblog.dao.SiteDAO;
import me.azthink.myblog.domain.Article;
import me.azthink.myblog.domain.Nav;
import me.azthink.myblog.domain.Site;

@WebServlet("/category.do")
public class CategoryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String categoryId = request.getParameter("id");

		// 1.从SiteDAO获取站点信息
		SiteDAO siteDAO = new SiteDAO();
		NavDAO navDAO = new NavDAO();
		ArticleDAO articleDAO = new ArticleDAO();
		try {
			Site site = siteDAO.findById(1);
			List<Nav> navList = navDAO.findAll();
			//List<Article> articleList = articleDAO.findAll();
			List<Article> articleList = articleDAO.findByCategory(Integer.valueOf(categoryId));
			List<Article> articleBannerList = articleDAO.findFeaturedFirst(4);

			// 2.将站点信息保存到请求域
			request.setAttribute("site", site);
			request.setAttribute("navList", navList);
			request.setAttribute("articleList", articleList);
			request.setAttribute("articleBannerList", articleBannerList);

			// 3.请求转发index.jsp
			request.getRequestDispatcher("/front/category.jsp").forward(request, response);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
